import {
	Config
} from './config.js'

class HTTP {
	constructor() {

	}

	request({
		url,
		data = {},
		method = 'GET'
	}) {
		return new Promise((resolve, reject) => {
			this._request(url, resolve, reject, data, method)
		})
	}

	_request(url, resolve, reject, data = {}, method = 'GET') {
		const finalUrl = Config.getInstance().getBaseUrl() + url;
		const token = uni.getStorageSync('access_token') || ''
		uni.request({
			url: finalUrl,
			method: method,
			timeout: 15000,
			data: data,
			header: {
				'Authorization': token,
				'content-type': 'application/json',
				'requestSource': '2',
			},
			success: (res) => {
				console.log(res.data)
				const data = res.data
				if (data.resultCode == "000000") {
					resolve(data)
				} else if (data.resultCode == "999999") {
					console.log("网络结果 999999", data)
					reject(data)
					uni.showToast({
						title: data.resultDesc,
						icon:'none',
						duration: 2000
					})
				}
			},
			fail: (err) => {
				console.error(err)
				const errorMes = err.errMsg
				reject(errorMes)
				uni.showToast({
					title: errorMes,
					icon:'none',
					duration: 2000
				})
			}

		})
	}

}

export{
	HTTP
}